unit uPosHandle;

interface
uses Classes,db,ADODB,GlobalObject,SysUtils;

type
  TPosItem = class(TCollectionItem)
  private
    FPosID: integer;
    FWID: Integer;
    FSelectFlag: Boolean;
    FW_Name: string;
    procedure SetPosID(const Value: integer);
    procedure SetWID(const Value: Integer);
    procedure SetSelectFlag(const Value: Boolean);
    procedure SetW_Name(const Value: string);
  public
    property WID: Integer read FWID write SetWID;
    property W_Name: string read FW_Name write SetW_Name;
    property PosID: integer read FPosID write SetPosID;
    property SelectFlag: Boolean read FSelectFlag write SetSelectFlag;
  end;

  TGroup = class(TCollection)
  private
    FTopGroupID: integer;
    FSmallGroupName: string;
    FTopGroupName: String;
    FSmallGroupID: integer;
    Fqry: TADoquery;
    FLevel: Integer;
    procedure SetSmallGroupID(const Value: integer);
    procedure SetSmallGroupName(const Value: string);
    procedure SetTopGroupID(const Value: integer);
    procedure SetTopGroupName(const Value: String);
    function Getitem(index: Integer): TPosItem;
    procedure Setitem(index: Integer; const Value: TPosItem);
    procedure SetLevel(const Value: Integer);
  public
    Constructor Create;
    Destructor Destroy; override;
    procedure AddPos(aWID,aPosID:integer);
    procedure LoadPos;
    procedure DeletePos(aPosID: Integer);
    procedure ModifPos(aOldPosID,aNewPosID: Integer);

    property item[index: Integer]: TPosItem read Getitem write Setitem;
    property TopGroupName: String read FTopGroupName write SetTopGroupName;
    property SmallGroupName: string read FSmallGroupName write SetSmallGroupName;
    property TopGroupID: integer read FTopGroupID write SetTopGroupID;
    property SmallGroupID: integer read FSmallGroupID write SetSmallGroupID;
    property Level: Integer read FLevel write SetLevel;
  end;



implementation

{ TGroup }

procedure TGroup.AddPos(aWID,aPosID:integer);
begin
  try
    Fqry.close;
    Fqry.SQL.Text := 'insert into Tab_S_GroupToPos(GroupID,W_ID,PosID,Updated)'+
      ' value('+inttostr(FSmallGroupID)+inttostr(aWID)+inttostr(aPosID)+',0)';
    Fqry.Open;
  except
  
  end;
end;

constructor TGroup.Create;
begin
  inherited Create(TPosItem);
  Fqry := TADOQuery.Create(nil);
  Fqry.Connection := GetMainDBConnection;
end;

procedure TGroup.DeletePos(aPosID: Integer);
var
  i,place: integer;
begin

  try
    Fqry.Close;
    Fqry.SQL.Text := 'delete from Tab_S_GroupToPos where GroupID = '+
      IntToStr(FSmallGroupID)+' and Pos_ID = ' + inttostr(aPosID);
    Fqry.Open;

    for i := 0 to Count - 1 do
      if TPosItem(Items[i]).PosID = aPosID then
        place := i;

    for i := place to count - 2 do
      Items[i] := Items[i+1];

    Items[Count-1].Free;
  except

  end;
end;

destructor TGroup.Destroy;
begin
  Fqry.free;
  inherited;
end;


function TGroup.Getitem(index: Integer): TPosItem;
begin
  Result := TposItem(inherited Items[Index]);
end;

procedure TGroup.LoadPos;
var
  i: integer;
  aPos: TPosItem;
begin

  try
    FQry.Close;
    FQry.SQL.Text := 'select * from View_S_DefineGroupPos where (W_ID = '
      + inttostr(FSmallGroupID)
      + ') order by GroupID';
    FQry.Open;
    clear;
    for i := 0 to FQry.RecordCount - 1 do
    begin
      aPos := TPosItem(add);
      aPos.PosID := FQry.FieldByName('Pos_ID').AsInteger;
      aPos.WID := Fqry.FieldByName('W_ID').AsInteger;
      aPos.W_Name := Fqry.FieldByName('W_Name').AsString;
      FQry.Next;
    end;
  except

  end;
end;

procedure TGroup.ModifPos(aOldPosID,aNewPosID: Integer);
var
  i: integer;
begin
   Fqry.Close;
   Fqry.SQL.Text := 'update Tab_S_Group set Pos_ID ='+inttostr(aNewPosID)+' where '
     +' GroupID = '+inttostr(FSmallGroupID)+' and PosID = '+inttostr(aOldPosID);
   Fqry.Open;

   for i := 0 to Count - 1 do
   begin
     if TPosItem(Items[i]).PosID = aOldPosID  then
       TPosItem(Items[i]).PosID := aNewPosID;
   end;
   
end;

procedure TGroup.Setitem(index: Integer; const Value: TPosItem);
begin
  Inherited SetItem(index, Value);
end;

procedure TGroup.SetLevel(const Value: Integer);
begin
  FLevel := Value;
end;

procedure TGroup.SetSmallGroupID(const Value: integer);
begin
  FSmallGroupID := Value;
end;

procedure TGroup.SetSmallGroupName(const Value: string);
begin
  FSmallGroupName := Value;
end;

procedure TGroup.SetTopGroupID(const Value: integer);
begin
  FTopGroupID := Value;
end;

procedure TGroup.SetTopGroupName(const Value: String);
begin
  FTopGroupName := Value;
end;

{ TPosItem }

procedure TPosItem.SetPosID(const Value: integer);
begin
  FPosID := Value;
end;

procedure TPosItem.SetSelectFlag(const Value: Boolean);
begin
  FSelectFlag := Value;
end;

procedure TPosItem.SetWID(const Value: Integer);
begin
  FWID := Value;
end;

procedure TPosItem.SetW_Name(const Value: string);
begin
  FW_Name := Value;
end;

end.
